//
//  LineTableViewTableViewCell.swift
//  FrameProduct
//
//  Created by delegate on 2025/10/21.
//

import UIKit

class LineTableViewTableViewCell: UITableViewCell {
    var note: NoteModel = NoteModel()
    
    var selectedDidTapActionBlock: ((NoteModel) -> Void)?
    var collectedDidTapActionBlock: ((NoteModel) -> Void)?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        
        setupUI()
        
    }
    func configure(with note: NoteModel, isHiddenBottomLineView: Bool) {
        self.note = note
        bottomLineView.isHidden = isHiddenBottomLineView
        selectButton.isSelected = note.selected
        titleLabel.text = note.noteLineTitle
        subTitleLabel.text = note.noteLineSubTitle
        speedLabel.text = note.noteSpeed
        progressView.progress = Float(note.noteSpeedPercent)
        collectedButton.isSelected = note.collected
        percentageLabel.text = String(format: "%d%%", note.noteSpeedPercent)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        contentView.addSubviews(selectButton, titleLabel, subTitleLabel, speedLabel, progressView, percentageLabel, collectedButton, topLineView, bottomLineView)
        topLineView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(68)
            make.height.equalTo(1)
            make.right.equalToSuperview()
        }
        selectButton.snp.makeConstraints { make in
            make.top.equalTo(topLineView.snp.bottom).offset(24)
            make.left.equalTo(25)
            make.width.height.equalTo(26)
        }
        bottomLineView.snp.makeConstraints { make in
            make.top.equalTo(selectButton.snp.bottom).offset(26)
            make.left.equalTo(topLineView.snp.left)
            make.right.equalTo(topLineView.snp.right)
            make.height.equalTo(1)
            make.bottom.equalToSuperview()
        }
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(selectButton.snp.right).offset(17)
            make.top.equalTo(14)
            make.height.equalTo(21)
        }
        collectedButton.snp.makeConstraints { make in
            make.right.equalTo(-23)
            make.centerY.equalTo(titleLabel.snp.centerY)
            make.width.height.equalTo(16)
        }
        progressView.snp.makeConstraints { make in
            make.right.equalTo(collectedButton.snp.left).offset(-14)
            make.centerY.equalTo(collectedButton.snp.centerY)
            make.height.equalTo(12)
            make.width.equalTo(56)
        }
        percentageLabel.snp.makeConstraints { make in
            make.top.equalTo(progressView.snp.top)
            make.bottom.equalTo(progressView.snp.bottom)
            make.centerX.equalTo(progressView.snp.centerX)
        }
        speedLabel.snp.makeConstraints { make in
            make.centerY.equalTo(progressView.snp.centerY)
            make.right.equalTo(progressView.snp.left).offset(-10)
            make.height.equalTo(21)
        }
        subTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(5)
            make.left.equalTo(titleLabel.snp.left)
            make.right.equalTo(-20)
            make.height.equalTo(18)
        }
        
    }
    //=================================================================
    //                           事件处理
    //=================================================================
    // MARK: - 事件处理
    @objc private func selectButtonAction() {
        selectedDidTapActionBlock?(note)
    }
    @objc private func collectedButtonAction() {
        collectedDidTapActionBlock?(note)
    }
    
    //=================================================================
    //                            lazy
    //=================================================================
    // MARK: - lazy
    private lazy var selectButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "line_normal_icon"), for: .normal)
        button.setImage(UIImage(named: "line_selected_icon"), for: .selected)
        button.addTarget(self, action: #selector(selectButtonAction), for: .touchUpInside)
        return button
    }()
    private lazy var collectedButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "line_collect_normal_icon"), for: .normal)
        button.setImage(UIImage(named: "line_collect_selected_icon"), for: .selected)
        button.addTarget(self, action: #selector(collectedButtonAction), for: .touchUpInside)
        return button
    }()
    private lazy var titleLabel: UILabel = {
        let label = UILabel(frame: .zero)
        label.text = "外国朋友"
        label.textColor = .lxs.hex("#FFFFFF")
        label.font = .lxs.font(size: 14)
        return label
    }()
    private lazy var speedLabel: UILabel = {
        let label = UILabel(frame: .zero)
        label.text = "10s"
        label.textColor = .lxs.hex("#43CF7C")
        label.font = .lxs.font(size: 14)
        return label
    }()
    private lazy var progressView: UIProgressView = {
        let progressView = UIProgressView()
        progressView.layer.borderColor = UIColor.lxs.hex("#F24444").cgColor
        progressView.layer.borderWidth = 1
        progressView.layer.cornerRadius = 6.0
        progressView.backgroundColor = .clear
        progressView.progressTintColor = .lxs.hex("#F24444")
        progressView.progress = 0.5
        return progressView
    }()
    private lazy var percentageLabel: UILabel = {
        let label = UILabel(frame: .zero)
        label.text = "10%"
        label.textColor = .lxs.hex("#FFFFFF")
        label.font = .lxs.font(size: 10)
        return label
    }()
    private lazy var subTitleLabel: UILabel = {
        let label = UILabel(frame: .zero)
        label.text = "日本专线 / 支持 Netflix / Prime / HBO Max / 油管高清"
        label.textColor = .lxs.hex("#808080")
        label.font = .lxs.font(size: 12)
        return label
    }()
    private lazy var topLineView: UIView = {
        let view = UIView()
        view.backgroundColor = .lxs.hex("#FFFFFF", alpha: 0.05)
        return view
    }()
    private lazy var bottomLineView: UIView = {
        let view = UIView()
        view.backgroundColor = .lxs.hex("#FFFFFF", alpha: 0.05)
        return view
    }()
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
extension LineTableViewTableViewCell: ReuseIdentifiable {
    static var reuseIdentifier: String {
        return String(describing: self)
    }
    
    
}
